import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'
import cheerio from 'cheerio'
Vue.use(ElementUI)

Vue.config.productionTip = false


// new Vue({
//   router,
//   store,
//   render: h => h(App)
// }).$mount('#app')

const env = process.env.NODE_ENV === 'production'
let appList = []
if (!env) {
  appList = [
    {
      name: 'app1',
      host: 'http://localhost:8002'
    },
    {
      name: 'app2',
      host: 'http://localhost:8003'
    },
    {
      name: 'app3',
      host: 'http://localhost:8004'
    },
    {
      name: 'app4',
      host: 'http://localhost:8005'
    }
  ]
} else {
  appList = [
    {
      name: 'app1',
      host: 'http://mic.xxx.com/app1'
    },
    {
      name: 'app2',
      host: 'http://mic.xxx.com/app2'
    },
    {
      name: 'app3',
      host: 'http://mic.xxx.com/app3'
    },
    {
      name: 'app4',
      host: 'http://mic.xxx.com/app4'
    }
  ]
}

import MicServer from '../../single-spa/index'
// 安装微服务
MicServer.getInstance({
  _axios: axios,
  _vue: Vue,
  _cheerio: cheerio,
  appList: appList
}).then((mic) => {
  Vue.prototype.micServer = mic
  console.log('主项目 getInstance then', mic)
  mic.registerApp({
    el: 'app',
    name: 'main',
    app: {
      router,
      store,
      render: (h) => h(App)
    }
  })
  mic.startApp({
    name: 'main'
  }).then((res) => {
    // 加载项目，添加蒙版
    console.log('主项目 main 挂在成功，期望结果是界面显示主项目，成功的Vue实例是：', res)
    const path = window.location.pathname
    // 刷新会走这个地方
    res.$router.push('/')
    if (path !== '/') {
      const appName = path.slice(1,5)
      // 不是首页，直接跳转路由
      mic.pushRouter({appName, path}, (res) => {
        if (res.success) {
          res.instance.$router.push({
            path: path
          })
          // 这里其实不准确哦
          store.commit('set_activeName', appName)
        } else {
          res.$message.error(res.msg)
        }
      })
    }
  })
}).catch((err) => {
  console.log(err)
})
